package org.quickserver.net.GB268753QN;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import org.quickserver.net.model.GB268753QNS;
import org.quickserver.net.model.GBServerDataAll;
import org.quickserver.net.model.GBServerDataS;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.util.data.dataProcessing;


/**
 * 小黑盒水设备
 */
public class analysisMainS
{
  public static GB268753QNS init(ClientHandler handler, String[] strs) {
    String logStrAll = dataProcessing.strsToStr(strs);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    GBServerDataS ec = (GBServerDataS)handler.getClientData();
    GB268753QNS GBQN = new GB268753QNS();
    int strsLength = strs.length;
    /*int dataLength = dataProcessing.sumHexStringToInt10(strs[24], strs[25]);
    String crcData = strs[strsLength - 3];
    String crcCalc = dataProcessing.crc_8(dataProcessing.strsToStrs(strs, 2, strsLength - 4));
    String headTail = String.valueOf(strs[0]) + strs[1] + strs[strsLength - 2] + strs[strsLength - 1];
    String serialNumber = String.valueOf(strs[2]) + strs[3];
    String[] mainMata = dataProcessing.strsToStrs(strs, 27, strsLength - 4);
    String[] otherData = dataProcessing.strsToStrs(strs, 2, 23);
    String otherTime = String.valueOf(strs[11]) + strs[10] + strs[9] + strs[8] + strs[7] + strs[6];*/
    int otherCommand = dataProcessing.hexStringToInt10(strs[1]);
    //int typeMark = dataProcessing.hexStringToInt10(strs[27]);
    String[] yesHm = { "40", "00", "03", "06", "23" };
   /* String yesCrc8Str = String.valueOf(dataProcessing.strsToStr(otherData)) + yesHm[1] + yesHm[1] + yesHm[2];
    String yesCrc8 = dataProcessing.crc_8(dataProcessing.bytesToHexStrings(dataProcessing.strToBytes(yesCrc8Str)));
    String yeshm = String.valueOf(yesHm[0]) + yesHm[0] + dataProcessing.strsToStr(otherData) + yesHm[1] + yesHm[1] + yesHm[2] + 
      yesCrc8 + yesHm[4] + yesHm[4];
    String noCrc8Str = String.valueOf(dataProcessing.strsToStr(otherData)) + yesHm[1] + yesHm[1] + yesHm[3];
    String noCrc8 = dataProcessing.crc_8(dataProcessing.bytesToHexStrings(dataProcessing.strToBytes(noCrc8Str)));
    String nohm = String.valueOf(yesHm[0]) + yesHm[0] + dataProcessing.strsToStr(otherData) + yesHm[1] + yesHm[1] + yesHm[3] + 
      noCrc8 + yesHm[4] + yesHm[4];*/
    byte[] hm = new byte[0];
    System.out.println(otherCommand + "--" + "-----" + logStrAll);
    String unCode="";
	try {
		unCode = dataProcessing.fromHexString(dataProcessing.strsToStr(strs));
	} catch (Exception e3) {
		// TODO Auto-generated catch block
		e3.printStackTrace();
	}
    Object obj = GBServerDataAll.devMap.get(unCode);
    if ((otherCommand == 2 && !ec.getUniqueCode().equals("0")) ||obj != null ) {
      try {
        Object o = null;
        
        if(obj != null) {
        	Class<?> clazz = 
                    Class.forName("org.quickserver.net.GB268753QNS.control.SSB" + 26);
                  Method method = clazz.getMethod("SSB" + 26 + "OBJ", new Class[] { ClientHandler.class, 
                        String[].class });
                  o = method.invoke(clazz.newInstance(), new Object[] { handler, strs });
        }else if (otherCommand == 3) {
          Class<?> clazz = 
            Class.forName("org.quickserver.net.GB268753QNS.control.SSB" + otherCommand);
          Method method = clazz.getMethod("SSB" + otherCommand + "OBJ", new Class[] { ClientHandler.class, 
                String[].class});
          method.invoke(clazz.newInstance(), new Object[] { handler, strs });
        }else if (otherCommand == 2) {
            Class<?> clazz = 
                    Class.forName("org.quickserver.net.GB268753QNS.control.SSB" + otherCommand);
                  Method method = clazz.getMethod("SSB" + otherCommand + "OBJ", new Class[] { ClientHandler.class, 
                        String[].class});
                 method.invoke(clazz.newInstance(), new Object[] { handler, strs });
                }
        if (o == null) {
          GBQN = null;
          //hm = dataProcessing.strToBytes(yeshm);
        }
        else {
          GBQN = (GB268753QNS)o;
          //hm = dataProcessing.strToBytes(yeshm);
        }
      
      } catch (IllegalAccessException e) {

        
        GBQN = null;
      //  hm = dataProcessing.strToBytes(nohm);
      //  handler.sendClientBinary(hm);
        return GBQN;
      } catch (IllegalArgumentException e) {

        
        GBQN = null;
       // hm = dataProcessing.strToBytes(nohm);
        //handler.sendClientBinary(hm);
        return GBQN;
      } catch (InvocationTargetException e) {

        
        GBQN = null;
        //hm = dataProcessing.strToBytes(nohm);
        //handler.sendClientBinary(hm);
        return GBQN;
      } catch (InstantiationException e) {

        
        GBQN = null;
       // hm = dataProcessing.strToBytes(nohm);
        //handler.sendClientBinary(hm);
        return GBQN;
      } catch (NoSuchMethodException e1) {

        
        GBQN = null;
        //hm = dataProcessing.strToBytes(nohm);
        try {
			handler.sendClientBinary(hm);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return GBQN;
      } catch (SecurityException e1) {

        
        GBQN = null;
        //hm = dataProcessing.strToBytes(nohm);
        //handler.sendClientBinary(hm);
        return GBQN;
      } catch (ClassNotFoundException e2) {
        handler.sendSystemMsg("system Class org.quickserver.net.GB268753QN.control.SSB" + otherCommand +  "******ClassNotFoundException!!!");
        
        String logStr = "";
        for (int s = 0; s < strs.length; s++) {
          logStr = String.valueOf(logStr) + strs[s];
        }
        GBQN = null;
        //hm = dataProcessing.strToBytes(yeshm);
        //handler.sendClientBinary(hm);
        return GBQN;
      } 
    }
    try {
		handler.sendClientBinary(hm);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    return GBQN;
  }
}
